package top.zywork.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.zywork.common.BeanUtils;
import top.zywork.dao.GoodsCommentDAO;
import top.zywork.dos.GoodsCommentDO;
import top.zywork.dos.GoodsCommentPicDO;
import top.zywork.dto.GoodsCommentDTO;
import top.zywork.dto.GoodsCommentPicDTO;
import top.zywork.dto.GoodsOrderDTO;
import top.zywork.enums.GoodsOrderStatusEnum;
import top.zywork.service.AbstractBaseService;
import top.zywork.service.GoodsCommentPicService;
import top.zywork.service.GoodsCommentService;
import top.zywork.service.GoodsOrderService;
import top.zywork.vo.GoodsCommentVO;
import top.zywork.vo.ResponseStatusVO;

import javax.annotation.PostConstruct;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * GoodsCommentServiceImpl服务接口实现类<br/>
 *
 * 创建于2019-07-27<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@Service(value = "goodsCommentService")
public class GoodsCommentServiceImpl extends AbstractBaseService implements GoodsCommentService {

    private GoodsCommentDAO goodsCommentDAO;

    private GoodsCommentPicService goodsCommentPicService;

    private GoodsOrderService goodsOrderService;

    @Autowired
    public void setGoodsCommentDAO(GoodsCommentDAO goodsCommentDAO) {
        super.setBaseDAO(goodsCommentDAO);
        this.goodsCommentDAO = goodsCommentDAO;
    }

    @Autowired
    public void setGoodsCommentPicService(GoodsCommentPicService goodsCommentPicService) {
        this.goodsCommentPicService = goodsCommentPicService;
    }

    @Autowired
    public void setGoodsOrderService(GoodsOrderService goodsOrderService) {
        this.goodsOrderService = goodsOrderService;
    }

    @PostConstruct
    public void init() {
        super.init(GoodsCommentDO.class, GoodsCommentDTO.class);
    }

    @Override
    @Transactional
    public ResponseStatusVO userSaveBatch(List<GoodsCommentVO> goodsCommentVOList, Long orderId) {
        Object obj = goodsOrderService.getById(orderId);
        if (null == obj) {
            return ResponseStatusVO.error("订单不存在", null);
        }
        List<GoodsCommentPicDTO> goodsCommentPicDTOList = new ArrayList<>();
        // 保存评论同时保存评论的图片
        for (GoodsCommentVO goodsCommentVO : goodsCommentVOList) {
            goodsCommentDAO.save(goodsCommentVO);
            String[] imageList = goodsCommentVO.getImageList();
            if (imageList.length > 0) {
                // 有上传图片，需要保存图片
                for (String picUrl : imageList) {
                    // 将图片信息保存起来
                    GoodsCommentPicDTO goodsCommentPicDTO = new GoodsCommentPicDTO();
                    goodsCommentPicDTO.setCommentId(goodsCommentVO.getId());
                    goodsCommentPicDTO.setPicUrl(picUrl);
                    goodsCommentPicDTOList.add(goodsCommentPicDTO);
                }
            }
        }
        if (goodsCommentPicDTOList.size() > 0) {
            // 有上传图片才需要保存图片信息
            goodsCommentPicService.saveBatch(BeanUtils.copyListObj(goodsCommentPicDTOList, GoodsCommentPicDO.class));
        }
        // 保存完评论，更新订单
        GoodsOrderDTO goodsOrderDTO = BeanUtils.copy(obj, GoodsOrderDTO.class);
        goodsOrderDTO.setOrderStatus(GoodsOrderStatusEnum.ALREADY_COMMENT.getValue().byteValue());
        goodsOrderService.update(goodsOrderDTO);
        return ResponseStatusVO.ok("评价成功", null);
    }
}
